<?php


namespace api\zmt\controller;


use api\zmt\model\AdminModel;
use api\zmt\model\OrderModel;
use cmf\controller\RestBaseController;
use think\facade\Db;
use think\Request;

class PayController extends RestBaseController
{
    //账户余额支付
    public function walletPay(Request $request)
    {
        $orderId = input('post.order_id');
//        $payType = input('post.pay_type');
//        if($payType != '账户余额') {
//            $this->error('当前仅支持账户余额支付');
//        }
        $order = OrderModel::find($orderId);
        if(!$order) $this->error('订单不存在');
        if($order['status'] != 0) $this->error('该订单支付');
        //查询用户
        $user = AdminModel::find($request->id);
        //判断账户余额是否够
        if($user['wallet'] < $order['total_price']) {
            $this->error('账户余额不足');
        }
        Db::startTrans();
        $updateOrderStatus = false;
        $addMoneyLog = false;
        $updateUserWallet = false;
        try {
            //1.订单的status修改成1
            $order->status = 1;
            $order->pay_type = '账户余额';
            $order->save();
            $after =  $user['wallet'] - $order['total_price'];
            //2、流水记录
            money_log($order['user_id'],$order['total_price'] , $user['wallet'],$after, 1);
            //3.用户余额修改
            $user->wallet = $after;
            $user->save();

            Db::commit();
            $this->success('支付成功');
        } catch (\Exception $exception) {
            Db::rollback();
            $this->error($exception->getMessage());
        }
    }
}